// pages/movie-detail/movie-detail.js

import {
  converToCastString,
  convertToCastInfos
} from "../../utils/utils.js"

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movie: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    const mid = options.mid
    wx.request({
      url: app.gBaseUrl + "subject/" + mid,
      success: (res) => {
        console.log(res.data);

        // 处理数据
        this.processMovieData(res.data)

        // this.setData({
        //   movie: res.data
        // })
      }
    })
  },

  processMovieData(movie) {
    const data = {}
    data.directors = converToCastString(movie.directors)
    data.casts = converToCastString(movie.casts)
    data.images = movie.images.large
    data.title = movie.title
    data.subtitle = movie.countries[0] + '·' + movie.year
    data.wishCount = movie.wish_count
    data.comments = movie.comments_count
    data.originalTitle = movie.original_title
    data.rating = movie.rating.average
    data.genres = movie.genres.join("、")
    data.summary = movie.summary
    data.castsInfo = convertToCastInfos(movie.casts)

    this.setData({
      movie: data
    })
  },

  onViewPost() {
    wx.previewImage({
      urls: [this.data.movie.images.large]
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})